package ext.csc.component.numbergen.classification;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;

import ext.csc.component.numbergen.bean.RequestInfoContained;
import ext.csc.component.numbergen.connection.DBConn;
import ext.csc.component.numbergen.number.GeneratedNumber;
import ext.csc.component.numbergen.number.NumberInfoContained;
import ext.csc.component.numbergen.number.NumberRegisterManager;
import ext.csc.component.numbergen.utilities.CommonUtil;

public class ClassificationHelper implements RemoteAccess, Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7237698703712806510L;

	/**
	public static Classification getClassification(String classificationPath){
		try {
			if (!RemoteMethodServer.ServerFlag) {
				return (Classification) RemoteMethodServer.getDefault().invoke("getClassificationRemote", ClassificationHelper.class.getName(), null,
						new Class[] {String.class},
						new Object[] {classificationPath});
			} else {
				return getClassificationRemote(classificationPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Classification();
	}**/
	
	/**
	 * Get a Classification Object from Database by a classificationID
	 * @param classificationID
	 * @return a Classification Object
	 * @throws Exception 
	 */
	public static Classification getClassification(String classificationPath){
		Classification resultClass = null;
		DBConn conn = null;
		
		try {
			conn = new DBConn();
			
			String sql = "SELECT * FROM csc_numberclassification WHERE " + Classification.CLASSIFICATION_PATH + " = '" + classificationPath + 
							"' ORDER BY " + Classification.CLASSIFICATION_PATH;
			ResultSet rs = conn.executeQuery(sql);
			
			if(rs.next()){
				resultClass = new Classification();
				String objectClassPath = rs.getString(Classification.CLASSIFICATION_PATH);
				String objectName = rs.getString(Classification.CLASSIFICATION_NAME);
				String objectClassValue = (String)CommonUtil.convertNull(rs.getString(Classification.CLASSIFICATION_VALUE));
				String objectClassDesc = (String)CommonUtil.convertNull(rs.getString(Classification.CLASSIFICATION_DESC));
				String objectParentClassPath = (String)CommonUtil.convertNull(rs.getString(Classification.CLASSIFICATION_PARENTPATH));
				
				resultClass.setObjectclasspath(objectClassPath);
				resultClass.setObjectname(objectName);
				resultClass.setObjectclassvalue(objectClassValue);
				resultClass.setObjectclassdesc(objectClassDesc);
				resultClass.setObjectparentclasspath(objectParentClassPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally{
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		
		return resultClass;
	}
	
	/**
	 * Get a Classification Object by a RequestInfoBean
	 * @param requestBean Bean from Presentation Layer
	 * @return a Classification Object
	 */
	public static Classification getClassification(ClassificationInfoContained requestBean){
		Classification resultClass = new Classification();
		
		resultClass.setObjectclasspath(requestBean.getObjectclasspath());
		resultClass.setObjectname(requestBean.getObjectname());
		resultClass.setObjectclassvalue(requestBean.getObjectclassvalue());
		resultClass.setObjectclassdesc(requestBean.getObjectclassdesc());
		
		return resultClass;
	}
	
	/**
	public static ArrayList getChildClassifications(String parentPath){
		try {
			if (!RemoteMethodServer.ServerFlag) {
				return (ArrayList) RemoteMethodServer.getDefault().invoke("getChildClassificationsRemote", ClassificationHelper.class.getName(), null,
						new Class[] {String.class},
						new Object[] {parentPath});
			} else {
				return getChildClassificationsRemote(parentPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList();
	}**/
	
	/**
	 * Get child Classificaiton nodes by parent node path
	 * @param parentPath String value of parent path
	 * @return ArrayList within child Classification objects
	 * @throws Exception 
	 */
	public static ArrayList getChildClassifications(String parentPath){
		ArrayList resultClassifications = new ArrayList();

		DBConn conn = null;
		try {
			conn = new DBConn();
			
			String sql = "SELECT * FROM csc_numberclassification WHERE " + Classification.CLASSIFICATION_PARENTPATH + " = '" + parentPath + 
							"' ORDER BY " + Classification.CLASSIFICATION_PATH;
			ResultSet rs = conn.executeQuery(sql);
			
			Classification resultClass;
			while(rs.next()){
				resultClass = new Classification(); 
				String objectClassPath = rs.getString(Classification.CLASSIFICATION_PATH);
				String objectName = rs.getString(Classification.CLASSIFICATION_NAME);
				String objectClassValue = (String)CommonUtil.convertNull(rs.getString(Classification.CLASSIFICATION_VALUE));
				String objectClassDesc = (String)CommonUtil.convertNull(rs.getString(Classification.CLASSIFICATION_DESC));
				
				resultClass.setObjectclasspath(objectClassPath);
				resultClass.setObjectname(objectName);
				resultClass.setObjectclassvalue(objectClassValue);
				resultClass.setObjectclassdesc(objectClassDesc);
				
				resultClassifications.add(resultClass);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally{
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}
		return resultClassifications;
	}
	
	/**
	 * Get child Classificaiton nodes by parent node
	 * @param parentClassification Classification object of parent node
	 * @return ArrayList within child Classification objects
	 */
	public static ArrayList getChildClassifications(ClassificationInfoContained parentClassification){
		String strClassificationParentPath = parentClassification.getObjectclasspath();
		return getChildClassifications(strClassificationParentPath);
	}

	public static ArrayList getUnregistedChildClassPath(String classificationPath){
		ArrayList result = new ArrayList();
		ArrayList intitalArray = new ArrayList();
		for (int i = 0; i < 9; i++) {
			intitalArray.add(i+"");
		}
		String classpath = "";
		ArrayList aChildClasses = getChildClassifications(classificationPath);
		
		if(aChildClasses.size() == 0) return result;
		
		for (int i = 0; i < aChildClasses.size(); i++) {
			Classification childClass = (Classification)aChildClasses.get(i);
			classpath = childClass.getObjectclasspath();
			String lastNumber = classpath.substring(classpath.length()-1, classpath.length());
			for (int k = 0; k <= 9; k++) {
				if(lastNumber.equals(k+"")){
					intitalArray.remove(k+"");
				}
			}
		}
		
		for (int i = 0; i < intitalArray.size(); i++) {
			result.add(classpath.substring(0, classpath.length()-1) + intitalArray.get(i));
		}
		
		return result;
	}
	
	/**
	public static void regClassification(ClassificationInfoContained classInfo){
		try {
			if (!RemoteMethodServer.ServerFlag) {
				RemoteMethodServer.getDefault().invoke("regClassificationRemote", ClassificationHelper.class.getName(), null,
						new Class[] {ClassificationInfoContained.class},
						new Object[] {classInfo});
			} else {
				regClassificationRemote(classInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}**/
	
	public static void regClassification(ClassificationInfoContained classInfo) throws Exception{
		String class_path = "'" + classInfo.getObjectclasspath() + "' ";
		String class_parent = "'" + classInfo.getObjectparentclasspath() + "' ";
		String class_name = "'" + classInfo.getObjectname() + "' ";
		String class_value = "'" + classInfo.getObjectclassvalue() + "' ";
		String class_desc = "'" + classInfo.getObjectclassdesc() + "' ";

		DBConn conn = null;

		try{
			conn = new DBConn();
			
			String sql;
			sql = "SELECT * FROM csc_numberclassification WHERE " + 
					Classification.CLASSIFICATION_PATH + " = " + class_path + 
					" AND " + Classification.CLASSIFICATION_PARENTPATH + " = " + class_parent;
			ResultSet rs = conn.executeQuery(sql);
			
			if(!rs.next()){
				sql = "INSERT INTO csc_numberclassification " + 
							"(" + Classification.CLASSIFICATION_PATH + "," + Classification.CLASSIFICATION_PARENTPATH + "," + 
							Classification.CLASSIFICATION_NAME + "," + Classification.CLASSIFICATION_VALUE + "," + 
							Classification.CLASSIFICATION_DESC + ") " +
							"VALUES(" + class_path + "," + class_parent + "," + class_name + "," + class_value + "," + class_desc + ")";
			}else{
				sql = "UPDATE csc_numberclassification SET " + 
						Classification.CLASSIFICATION_PARENTPATH + " = " + class_parent + ", " + 
						Classification.CLASSIFICATION_NAME + " = " + class_name + ", " + 
						Classification.CLASSIFICATION_VALUE + " = " + class_value +  ", " + 
						Classification.CLASSIFICATION_DESC + " = " + class_desc + 
						" WHERE " + 
						Classification.CLASSIFICATION_PATH + " = " + class_path;
			}

			conn.executeUpdate(sql);
			conn.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			conn.rollback();
			throw new Exception("*** New Classification Terminated!! ***  Class path: " + class_path + " Failed!");
		}finally{
			try {
				conn.close();
			} catch (Exception e) {
				throw e;
			}
		}
	}
	
	public static void updateClassification(String classPath, String name, String value) throws Exception{
		String class_path = "'" + classPath + "' ";
		String class_name = "'" + name + "' ";
		String class_value = "'" + value + "' ";
		
		DBConn conn = null;
		try{
			conn = new DBConn();
			
			String sql;
			sql = "UPDATE csc_numberclassification SET " + 
					Classification.CLASSIFICATION_NAME + " = " + class_name + ", " + 
					Classification.CLASSIFICATION_VALUE + " = " + class_value + 
					" WHERE " + 
					Classification.CLASSIFICATION_PATH + " = " + class_path;
		
			conn.executeUpdate(sql);
			conn.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			conn.rollback();
			throw new Exception("*** Update Classification Terminated!! ***  Class path: " + classPath + " Failed!");
		}finally{
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void deleteClassification(String classPath) throws Exception{
		try {
			if (!RemoteMethodServer.ServerFlag) {
				RemoteMethodServer.getDefault().invoke("deleteClassificationRemote", ClassificationHelper.class.getName(), null,
						new Class[] {String.class},
						new Object[] {classPath});
			} else {
				deleteClassificationRemote(classPath);
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static void deleteClassificationRemote(String classPath) throws Exception{
		String class_path = "'" + classPath + "' ";
		DBConn conn = null;
		
		try{
			conn = new DBConn();
			
			String sql;
			sql = "Delete FROM csc_numberclassification " + 
				" WHERE " + 
				Classification.CLASSIFICATION_PATH + " = " + class_path;

			conn.executeUpdate(sql);
			conn.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			conn.rollback();
			throw new Exception("*** Delete Classification Terminated!! ***  Class path: " + classPath + " Failed!");
		}finally{
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		ArrayList childClasses = ClassificationHelper.getChildClassifications(args[0]);
		Classification resultClass;
		for (int i = 0; i < childClasses.size(); i++) {
			resultClass = (Classification) childClasses.get(i);
			System.out.println("Classification_Path = " + resultClass.getObjectclasspath());
			System.out.println("Classification_Name = " + resultClass.getObjectname());
			System.out.println("Classification_Value = " + resultClass.getObjectclassvalue());
			System.out.println("Classification_Desc = " + resultClass.getObjectclassdesc());
		}
	}
}
